package com.yanqu.road.dao.impl.activity.seacraft;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.seacraft.pirateriot.config.SeacraftPirateRiotChapterConfig;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class SeacraftPirateRiotChapterConfigDaoImpl extends TempDao {

    private static String getSeacraftPirateRiotChapterConfigSql = "select * from `t_s_activity_seacraft_chapter` where `activityId`=?";

    public Map<Integer, SeacraftPirateRiotChapterConfig> getSeacraftPirateRiotChapterConfigMap(int activityId) {
        Map<Integer, SeacraftPirateRiotChapterConfig> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getSeacraftPirateRiotChapterConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                SeacraftPirateRiotChapterConfig seacraftPirateRiotChapterConfig = new SeacraftPirateRiotChapterConfig();
                seacraftPirateRiotChapterConfig.setActivityId(rs.getInt("activityId"));
                seacraftPirateRiotChapterConfig.setChapterId(rs.getInt("chapterId"));
                seacraftPirateRiotChapterConfig.setSeaId(rs.getInt("seaId"));
                seacraftPirateRiotChapterConfig.setReward(rs.getString("reward"));
                seacraftPirateRiotChapterConfig.setFailReward(rs.getString("failReward"));
                seacraftPirateRiotChapterConfig.setFirstReward(rs.getString("firstReward"));

                String randomReward = rs.getString("randomReward");
                seacraftPirateRiotChapterConfig.setRandomReward(StringUtils.isNullOrEmpty(randomReward) ? "0" : randomReward);

                seacraftPirateRiotChapterConfig.setEnergy(rs.getInt("energy"));
                seacraftPirateRiotChapterConfig.setHide(rs.getInt("hide"));
                seacraftPirateRiotChapterConfig.setAdviceAttack(rs.getLong("adviceAttack"));
                seacraftPirateRiotChapterConfig.setAdviceHp(rs.getLong("adviceHp"));
                map.put(seacraftPirateRiotChapterConfig.getChapterId(), seacraftPirateRiotChapterConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }


}
